package com.xb.xbpm.job;

import com.xb.xbpm.utils.EsUtils;
import com.xb.xbpm.utils.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import sun.rmi.runtime.NewThreadAction;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 每隔10分钟 从elasticsearch获取数据——>mysql
 * @Author gxd
 * @Date： 2018/8/27
 */
@Component
@EnableScheduling//可以在启动类上注解也可以在当前文件
public class InitDataTask {
    @Autowired
    EsUtils esUtils;

    /**
     * 0 0/10 * * * ?
     * pv定时任务
     */
    @Async
    @Scheduled(cron = "0 20 9 * * ?")
    public void runPv(){
        /*
         *1.查询所有索引
         *2.该索引下所有的siteid
         *3.根据index siteid 查询pv uv ip ov ——>mysql
         */
        //当天所有的索引
        List<String> indexs=esUtils.getAllIndices();
        for (String s : indexs) {
            //查询所有的siteId
            List<String> siteids=esUtils.getAllSiteIdByIndex(s);
            for (String s1: siteids) {
                if("".equals(s1)){
                    continue;
                }
                //根据index siteid 查询pv uv ip ov ——>mysql
                int curtime=Integer.valueOf(Tools.date2Str(new Date(),"HH"));//当前时间19:00取19
                String today=Tools.date2Str(new Date(),"yyyy-MM-dd");
                boolean flag=esUtils.getPvToSave(s,s1,today,curtime);//PV数据入库
            }
        }
    }


    /**
     * uv定时任务
     *0 0/10 * * * ? 每10分钟
     */
    @Async
    @Scheduled(cron = "0 20 9 * * ?")
    public void runUv(){
        /*
         *1.查询所有索引
         *2.该索引下所有的siteid
         *3.根据index siteid 查询pv uv ip ov ——>mysql
         */
        //当天所有的索引
        List<String> indexs=esUtils.getAllIndices();
        for (String s : indexs) {
            //查询所有的siteId
            List<String> siteids=esUtils.getAllSiteIdByIndex(s);
            for (String s1: siteids) {
                if("".equals(s1)){
                    continue;
                }
                //根据index siteid 查询pv uv ip ov ——>mysql
                int curtime=Integer.valueOf(Tools.date2Str(new Date(),"HH"));//当前时间19:00取19
                String today=Tools.date2Str(new Date(),"yyyy-MM-dd");
                boolean flag=esUtils.getUvToSave(s,s1,today,curtime);//UV数据入库
            }
        }
    }

    /**
     * 0 0/10 * * * ?
     * ip定时任务
     */
    @Async
    @Scheduled(cron = "0 20 9 * * ?")
    public void runIp(){
        /*
         *1.查询所有索引
         *2.该索引下所有的siteid
         *3.根据index siteid 查询pv uv ip ov ——>mysql
         */
        //当天所有的索引
        List<String> indexs=esUtils.getAllIndices();
        for (String s : indexs) {
            //查询所有的siteId
            List<String> siteids=esUtils.getAllSiteIdByIndex(s);
            for (String s1: siteids) {
                if("".equals(s1)){
                    continue;
                }
                //根据index siteid 查询pv uv ip ov ——>mysql
                int curtime=Integer.valueOf(Tools.date2Str(new Date(),"HH"));//当前时间19:00取19
                String today=Tools.date2Str(new Date(),"yyyy-MM-dd");
                boolean flag=esUtils.getIpsToSave(s,s1,today,curtime);//ips数据入库
            }
        }
    }



    /**0 0/10 * * * ?
     * 新增uv定时任务
     */
    @Async
    @Scheduled(cron = "0 20 9 * * ?")
    public void runNewUv(){
        /*
         *1.查询所有索引
         *2.该索引下所有的siteid
         *3.根据index siteid 查询pv uv ip ov ——>mysql
         */
        //当天所有的索引
        List<String> indexs=esUtils.getAllIndices();
        for (String s : indexs) {
            String [] strs=s.split("_");
            String userid=strs[1];
            if(Tools.isEmpty(userid)){
                continue;
            }
            //查询所有的siteId
            List<String> siteids=esUtils.getAllSiteIdByIndex(s);
            for (String s1: siteids) {
                if("".equals(s1)){
                    continue;
                }
                //根据index siteid 查询pv uv ip ov ——>mysql
                int curtime=Integer.valueOf(Tools.date2Str(new Date(),"HH"));//当前时间19:00取19
                String today=Tools.date2Str(new Date(),"yyyy-MM-dd");


                String index2=getBeforDateIndex3(new Date(),userid);//不包括今天
                String todayIndex="xblog_"+userid+"_"+Tools.date2Str(new Date(),"yyyyMMdd");
                String index3=index2+todayIndex;
                long r2=esUtils.getTotalCountUv(index2,s1);//不包括今天的
                //新增独立访客  目前采取和前3天比较
                boolean flag=esUtils.getNewUvToSave(userid,s,s1,today,curtime,index3,r2);//newUv数据入库
            }
        }
    }



    /**
     * 获取3天前的索引
     * @return
     */
    private  String getBeforDateIndex3(Date end,String userid) {

        DateFormat format1 = new SimpleDateFormat("yyyyMMdd");
        String indexs="";
        List<String> result = new ArrayList<String>();
        Calendar tempStart = Calendar.getInstance();
        //Date end=new Date();
        Date start=null;

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(end);
        calendar.add(Calendar.DAY_OF_MONTH, -3);
        start = calendar.getTime();

        tempStart.setTime(start);
        tempStart.add(Calendar.DAY_OF_YEAR, 1);

        Calendar tempEnd = Calendar.getInstance();
        tempEnd.setTime(end);
        while (tempStart.before(tempEnd)) {
            indexs+="xblog_"+userid+"_"+format1.format(tempStart.getTime())+",";
            tempStart.add(Calendar.DAY_OF_YEAR, 1);
        }
        return indexs;
    }

}
